/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.spring.provider.init.manager;

import java.util.Collection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import com.genia.toolbox.basics.exception.technical.WrapperException;
import com.genia.toolbox.spring.provider.init.bean.SpringInitializable;

/**
 * This class handles the initialization of the application. It calls the init
 * method of all <code>SpringInitializable</code> defined in the spring config
 * file, in the order defined be the order property of
 * <code>SpringInitializable</code>.
 */
public class SpringInitializableManager
    implements ApplicationListener
{

  /**
   * the {@link Collection} of {@link SpringInitializable} to call when spring
   * is loaded.
   */
  private Collection<SpringInitializable> springInitializables;

  /**
   * Has the initialization already occured.
   */
  private transient boolean initialized = false;



  /**
   * The initialization method. It is called via the onApplicationEvent method.
   * 
   * @param applicationContext
   *          the <code>ApplicationContext</code> to initialize.
   */
  public void init(final ApplicationContext applicationContext)
  {
    if (initialized) {
      return;
    }
    initialized = true;    
    for (final SpringInitializable initializable : getSpringInitializables()) {
      try {
        initializable.init();
      }
      catch (final RuntimeException e) {
        throw e;
      }
      catch (final Exception e) {
        throw new WrapperException(e);
      }
    }
  }



  /**
   * This method is called on each event on the <code>ApplicationContext</code>.
   * It is used to call all the initialization beans.
   * 
   * @param applicationEvent
   *          the event being fired
   * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
   */
  public void onApplicationEvent(final ApplicationEvent applicationEvent)
  {

    if (applicationEvent instanceof ContextRefreshedEvent) {
      init(((ContextRefreshedEvent) applicationEvent).getApplicationContext());
    }
  }



  /**
   * getter for the springInitializables property.
   * 
   * @return the springInitializables
   */
  public Collection<SpringInitializable> getSpringInitializables()
  {
    return springInitializables;
  }



  /**
   * setter for the springInitializables property.
   * 
   * @param springInitializables
   *          the springInitializables to set
   */
  public void setSpringInitializables(Collection<SpringInitializable> springInitializables)
  {
    this.springInitializables = springInitializables;
  }

}
